<template>
  <div>
    <el-table v-loading="loading" :data="couponCodeList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="发放的用户" align="center" prop="nickName" />
      <el-table-column label="订单编号" align="center" prop="orderSn" />
      <el-table-column label="券码" align="center" prop="couponCode" />
      <el-table-column label="卡号" align="center" prop="couponCard" />
      <el-table-column label="对应的券名" align="center" prop="couponName" />
      <el-table-column label="券码金额" align="center" prop="couponPrice" />
      <el-table-column
        v-if="couponType == 1"
        label="核销商户"
        align="center"
        prop="shoperName"
      />
      <el-table-column label="核销时间" align="center" prop="useTime" />
      <el-table-column label="作废状态" align="center">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isDel == 2">已作废</el-tag>
          <el-tag type="success" v-else>正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="作废时间" align="center" prop="delTime" />
      <el-table-column label="券码状态" align="center" prop="isStatus">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isStatus"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
            v-hasPermi="['cancel:couponCode:updateStatus']"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="过期时间" align="center" prop="outTime">
        <template slot-scope="scope">
          {{ scope.row.outTime }}
          <el-button
            size="medium"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['cancel:couponCode:updateStatusOutTime']"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isDel == 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cancel:couponCode:delCode']"
            >作废</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="修改时间" :visible.sync="isOpen" width="600px">
      <el-form ref="form" :model="form" :rules="rule" label-width="100px">
        <el-form-item label="过期时间" prop="outTime">
          <el-date-picker
            v-model="form.outTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择过期时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="isOpen = false">取 消</el-button>
        <el-button type="primary" @click="subActive">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  delCode,
  updateStatus,
  updateStatusOutTime,
  listCouponCode,
} from "@/api/system/couponCode";
export default {
  name: "couponDetail",
  props: {
    couponType: {
      default: 1,
    },
    couponCodeList: {
      type: Array,
    },
    total: {
      default: 0,
    },
    queryParams: {
      type: Object,
    },
    loading: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      isOpen: false,
      rule: {
        outTime: [
          { required: true, message: "请选择过期时间", trigger: "blur" },
        ],
      },
      form: {},
    };
  },
  created() {},
  methods: {
    getList() {
      this.loading = true;
      console.log(this.$route.query);
      this.queryParams.cid = this.$route.query.cid;
      listCouponCode(this.queryParams).then((response) => {
        this.couponCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleEdit(row) {
      const { id } = row;
      this.form = {
        id,
      };
      this.isOpen = true;
    },
    subActive() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateStatusOutTime(this.form).then((res) => {
            this.$modal.msgSuccess("操作成功");
            this.isOpen = false;
            this.$emit("form", res);
          });
        }
      });
    },
    handleDelete(row) {
      const { id, couponName } = row;

      this.$modal
        .confirm("作废后将不可恢复，是否确认作废" + couponName + "？")
        .then(function () {
          return delCode({ id });
        })
        .then((res) => {
          this.$emit("change", res);
          this.$modal.msgSuccess("此券已作废");
        })
        .catch(() => {});
    },
    // 是否可用
    handleStatusChange(row) {
      const { id, isStatus } = row;
      let text = row.isStatus == 1 ? "激活" : "关闭";
      this.$modal
        .confirm("确认要" + text + "吗？")
        .then(function () {
          return updateStatus({ id, isStatus });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isStatus = row.isStatus == 1 ? 0 : 1;
        });
    },
  },
};
</script>
<style></style>
